package demo1;

class Money implements Cloneable{
    public double money = 12.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable{
    public String name;
    public int age;
    public Money m;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person)super.clone();
        tmp.m = (Money)this.m.clone();
        return tmp;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        m = new Money();
    }


    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


public class test1  {
    public static void main(String[] args) throws CloneNotSupportedException  {
        Person person1 = new Person("zhangsan",13);
        Person person2 = (Person)person1.clone();
        System.out.println("修改之前" + person1.m.money);
        System.out.println("修改之前" + person2.m.money);
        person2.m.money = 99;
        System.out.println("修改之后" + person1.m.money);
        System.out.println("修改之后" + person2.m.money);

    }

    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person("zhangsan",13);
        Person person2 = (Person)person1.clone();
        System.out.println(person1.toString());
        System.out.println(person2.toString());
    }
}
